﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using CNRegex.Web.Base;
using CNRegex.Biz.BizAccess;
using CNRegex.Enum;
using System.Configuration;
using CNRegex.Entity;
using System.Text;
using CNRegex.Web.Utility;
using CNRegex.Entity.EntityEx;
using Project.Common;
using CNRegex.Biz;

namespace CNRegex.Web.Models
{
	public class ModelRegexTutorial:BaseModel
	{
		private BizRegexTutorialCatalog bizRegexTutorialCatalog;
		private BizRegexTutorialCatalogContent bizRegexTutorialCatalogContent;
		private BizRegexTutorialCatalogContentComment bizRegexTutorialCatalogContentComment;
		private BizRegexTutorial bizRegexTutorial;
		private Guid regexTutorialID;
		private Guid? currentRegexCatalogID;
		private RegexTutorial regexTutorial;
		private RegexTutorialCatalogContent currentRegexTutorialCatalogContent;

		public Guid? CurrentRegexCatalogID
		{
			get { return currentRegexCatalogID; }
			set { currentRegexCatalogID = value; }
		}

		public int? PageIndex { get; set; }

		public RegexTutorialCatalogContent CurrentRegexTutorialCatalogContent
		{
			get
			{
				if (currentRegexTutorialCatalogContent != null)
					return currentRegexTutorialCatalogContent;
				if (currentRegexCatalogID == null)
				{
					var temp = regexTutorialCatalog.FirstOrDefault(x => x.IsDefault == true);
					currentRegexTutorialCatalogContent = temp == null ? new RegexTutorialCatalogContent() : bizRegexTutorialCatalogContent.ListBy(x=>x.TutorialCatalogID==temp.ID,"", EnumOrder.ASC).First();
				}
				else
				{
					currentRegexTutorialCatalogContent = bizRegexTutorialCatalogContent.ListBy(x => x.TutorialCatalogID == currentRegexCatalogID,"", EnumOrder.ASC).FirstOrDefault() ?? new RegexTutorialCatalogContent();
				}
				return currentRegexTutorialCatalogContent;
			}
		}


		public RegexTutorial RegexTutorial
		{
			get { return regexTutorial; }
		}
		private IList<RegexTutorialCatalog> regexTutorialCatalog;

		public ModelRegexTutorial()
		{
			regexTutorialID = Guid.Parse(ConfigurationManager.AppSettings.Get(EnumAppSettings.RegexTutorialID.ToString()));
			regexTutorial = bizRegexTutorial.GetByID(regexTutorialID);
			regexTutorialCatalog = bizRegexTutorialCatalog.ListByTutorialID(regexTutorialID);
		}

		private PaginateHelper paginate;
		public PaginateHelper Paginate
		{
			get
			{
				currentRegexTutorialCatalogContentComments = CurrentRegexTutorialCatalogContentComments;
				return paginate;
			}
		}

		private IList<RegexTutorialCatalogContentCommentEx> currentRegexTutorialCatalogContentComments;
		public IList<RegexTutorialCatalogContentCommentEx> CurrentRegexTutorialCatalogContentComments
		{
			get
			{
				if (currentRegexTutorialCatalogContentComments != null)
					return currentRegexTutorialCatalogContentComments;
				int count = 0;
				currentRegexTutorialCatalogContentComments = bizRegexTutorialCatalogContentComment.List(ProjectGlobal.PAGE_SIZE, PageIndex, CurrentRegexTutorialCatalogContent.ID, ref count);
				paginate = new PaginateHelper(ProjectGlobal.PAGE_SIZE, count, PageIndex ?? 0, string.Format("/RegexTutorial/Index/?id={0}&", CurrentRegexTutorialCatalogContent.TutorialCatalogID));
				paginate.EmptyTip = "暂无评论,简单讨论一下吧～～";
				return currentRegexTutorialCatalogContentComments;
			}
		}

		public string RightCategoryContent
		{
			get
			{
				StringBuilder sb = new StringBuilder();
				IList<RegexTutorialCatalog> tempList = regexTutorialCatalog.Where(x => x.Level == 0).ToList();
				sb.Append("<ul class='rti_category_ul'>");
				for (int i = 0; i < tempList.Count; i++)
				{
					sb.Append(Recursive(tempList[i]));
				}
				sb.Append("</ul>");
				return sb.ToString();
			}
		}

		private string Recursive(RegexTutorialCatalog catalog)
		{
			StringBuilder treeContent = new StringBuilder();
			if (catalog.IsLeaf.Value)
			{
				if (CurrentRegexTutorialCatalogContent.TutorialCatalogID == catalog.ID)
					return string.Format(@"<li class='rti_categoryLeaf'>
										<span>{0}</span>
									</li>", catalog.Name, catalog.ID);
				else
					return string.Format(@"<li class='rti_categoryLeaf'>
										<a href='/RegexTutorial/index/{1}'>{0}</a>
									</li>", catalog.Name, catalog.ID);

			}
			IList<RegexTutorialCatalog> tempList = regexTutorialCatalog.Where(x => x.ParentID == catalog.ID).ToList();
			for (int i = 0; i < tempList.Count; i++)
			{
				string content = Recursive(tempList[i]);
				treeContent.Append(content);
			}
			treeContent.Insert(0, string.Format(@"
											<li class='rti_catalog_li'>
												<a href='javascript:void(0)' class='rti_categoryNonLeaf' style='color:black'>{0}</a>
												<ul class='rti_subCategoryUl'>
													", catalog.Name));
			treeContent.Append("</ul>");
			treeContent.Append("</li>");
			return treeContent.ToString();
		}
	}
}